package com.ruoyi.tester.executor.http;

import org.json.JSONObject;
import org.springframework.util.StringUtils;

import java.util.Map;

public interface HttpUtils {
    XzfHttpResult get(String url, String contentType, Map<String, String> headers, String queryParam, String pathParam);
    XzfHttpResult head(String url, String contentType, Map<String, String> headers, String queryParam, String pathParam);
    XzfHttpResult post(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam);
    XzfHttpResult put(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam);
    XzfHttpResult delete(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam);
    XzfHttpResult patch(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam);
    XzfHttpResult trace(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam);
    XzfHttpResult options(String url, String contentType, Map<String, String> headers, String queryParam, String bodyParam, String pathParam);
    default String buildQueryParamUrl(String url, String queryParam){
        boolean alreadHasParam = url.indexOf('?') > -1;
        if(StringUtils.hasLength(queryParam)) {
            StringBuilder sb = new StringBuilder(url);
            JSONObject paramJson = new JSONObject(queryParam);
            int i = 0;
            for (String key : paramJson.keySet()) {
                if (i == 0 && !alreadHasParam) {
                    sb.append('?');
                } else {
                    sb.append('&');
                }
                sb.append(key).append('=').append(paramJson.get(key));
                i++;
            }
            url = sb.toString();
        }
        return url;
    }
}
